package org.finesys.system.handler;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.finesys.common.redis.core.mq.client.RedisMQTemplate;
import org.finesys.system.api.dto.RouteConfDto;
import org.finesys.system.api.entity.SysRouteConf;
import org.finesys.system.api.route.mq.RouteMessage;
import org.finesys.system.convert.SysRouteConfConvert;
import org.finesys.system.service.SysRouteConfService;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化加载动态路由信息
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DynamicRouteLoadHandler implements SmartInitializingSingleton {

    private final SysRouteConfService sysRouteConfService;
    private final RedisMQTemplate redisMQTemplate;

    @Override
    public void afterSingletonsInstantiated() {
        //获取所有的路由信息
        List<SysRouteConf> routeConfs = sysRouteConfService.findAll();
        if (log.isDebugEnabled()) {
            log.debug("加载到路由信息：", routeConfs);
        }
        List<RouteConfDto> route = SysRouteConfConvert.INSTANCE.convertList(routeConfs);
        redisMQTemplate.send(new RouteMessage(route));
    }
}
